package controllers_menu

import (
	"OPS_TEMPLATE/models"
	"OPS_TEMPLATE/util/html"
	"github.com/astaxie/beego"
)

type TableController struct {
	beego.Controller
}

func (this *TableController) URLMapping() {
	this.Mapping("menusindex", this.MenusIndex)
	this.Mapping("menusadd", this.MenusAdd)
	this.Mapping("menusdel", this.MenusDel)
	this.Mapping("menusedit", this.MenusEdit)
	this.Mapping("getmenu", this.GetMenu)
	this.Mapping("menusave", this.MenusSave)
	this.Mapping("lists", this.ListsIndex)
	this.Mapping("listsadd", this.ListsAdd)
	this.Mapping("listsdel", this.ListsDel)
	this.Mapping("listsedit", this.ListsEdit)
	this.Mapping("listsave", this.ListsSave)
	this.Mapping("listpreview", this.ListsPreview)
}

// @router /menu/menus/manager [post]
func (this *TableController) MenusIndex() {
	this.TplName = "menu/menumanager.html"
}

// @router /menu/menus/add [post]
func (this *TableController) MenusAdd() {

	this.TplName = "menu/menumanager_form.html"
}

// @router /menu/menus/delete [post]
func (this *TableController) MenusDel() {
	beego.Debug(this.GetString("ids"))
	sql := "delete from menu where id in (" + this.GetString("ids") + ")"
	_, err := models.GetOrm().Raw(sql).Exec()
	if err != nil {
		beego.Error(err)
	}
	this.TplName = "menu/menumanager.html"
}

// @router /menu/menus/edit [post]
func (this *TableController) MenusEdit() {
	var menutable models.Menu
	menutable.Id, _ = this.GetInt("id")
	models.GetOrm().Read(&menutable)
	this.Data["allmenu"] = menutable.GetAllmenus(menutable.Grade - 1)
	this.Data["menutable"] = menutable
	this.TplName = "menu/menumanager_form.html"
}

// @router /menu/menus/getmenu [post]
func (this *TableController) GetMenu() {
	var menu *models.Menu
	if grade, err := this.GetInt("s"); err == nil && grade != 0 && grade != 1 {
		this.Data["json"] = menu.GetAllmenus(grade - 1)
	} else {
		beego.Error(err)
		beego.Debug(grade)
		this.Data["json"] = "get grade error"
	}
	this.ServeJSON()
}

// @router /menu/menus/menusave [post]
func (this *TableController) MenusSave() {
	menutable := models.Menu{}
	err := this.ParseForm(&menutable)
	if err == nil {
		res, errmap := menutable.Valid()
		if res {
			menutable.Type = "menu"
			if menutable.Id == 0 {
				_, err = models.GetOrm().Insert(&menutable)
			} else {
				beego.Debug(menutable.Pid)
				_, err = models.GetOrm().Update(&menutable)
			}
			if err != nil {
				this.Data["error"] = true
				this.Data["errTitle"] = err.Error()
				this.Data["allmenu"] = menutable.GetAllmenus(menutable.Grade - 1)
				this.Data["menutable"] = menutable
				this.TplName = "menu/menumanager_form.html"
				return
			}
		} else {
			this.Data["errmap"] = errmap
			this.Data["allmenu"] = menutable.GetAllmenus(menutable.Grade - 1)
			this.Data["menutable"] = menutable
			this.TplName = "menu/menumanager_form.html"
			return
		}

	} else {
		this.Data["error"] = true
		this.Data["errTitle"] = err.Error()
		this.TplName = "menu/menumanager_form.html"
		return
	}
	this.TplName = "menu/menumanager.html"
}

// @router /menu/lists/manager [post]
func (this *TableController) ListsIndex() {
	this.TplName = "menu/listmanager.html"
}

// @router /menu/lists/listsadd [post]
func (this *TableController) ListsAdd() {
	this.TplName = "menu/listmanager_form.html"
}

// @router /menu/lists/delete [post]
func (this *TableController) ListsDel() {
	beego.Debug(this.GetString("ids"))
	sql := "delete from tables where id in (" + this.GetString("ids") + ")"
	_, err := models.GetOrm().Raw(sql).Exec()
	if err != nil {
		beego.Error(err)
	}
	this.TplName = "menu/listmanager.html"
}

// @router /menu/lists/listsedit [post]
func (this *TableController) ListsEdit() {
	var listtable models.Tables
	listtable.Id, _ = this.GetInt("id")
	models.GetOrm().Read(&listtable)
	// beego.Debug(listtable)
	this.Data["listtable"] = listtable
	this.TplName = "menu/listmanager_form.html"
}

// @router /menu/lists/savelist [post]
func (this *TableController) ListsSave() {
	listtable := models.Tables{}
	err := this.ParseForm(&listtable)
	if err == nil {
		res, errmap := listtable.Valid()
		if res {
			if listtable.Id == 0 {
				_, err = models.GetOrm().Insert(&listtable)
			} else {
				_, err = models.GetOrm().Update(&listtable)
			}
			if err != nil {
				beego.Error(err)
			}
		} else {
			this.Data["errmap"] = errmap
			this.Data["listtable"] = listtable
			this.TplName = "menu/listmanager_form.html"
			return
		}

	} else {
		beego.Error(err)
	}
	this.TplName = "menu/listmanager.html"
}

// @router /menu/lists/listpreview [post]
func (this *TableController) ListsPreview() {
	code := this.GetString("code")
	beego.Debug(this.GetString("code"))
	this.Data["preview"] = util_html.GetListGrid(code, 1, "", "")
	this.TplName = "frame/_preview.html"

}
